// -*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-
//-
// Copyright (c) 2010 Michael Smith. All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.

/// @file       protocol.h
/// @brief      ArduPilotMega binary protocol

/// @name       GCS Message IDs
//@{
#define MSG_ACKNOWLEDGE 0x00
#define MSG_HEARTBEAT   0x01
#define MSG_ATTITUDE    0x02
#define MSG_LOCATION    0x03
#define MSG_PRESSURE    0x04
#define MSG_STATUS_TEXT 0x05
#define MSG_PERF_REPORT 0x06
#define MSG_COMMAND     0x22
#define MSG_GET_VALUE   0x30
#define MSG_SET_VALUE   0x31
#define MSG_VALUE       0x32
#define MSG_PID         0x42
#define MSG_TRIMS       0x50
#define MSG_MINS        0x51
#define MSG_MAXS        0x52
//@}

struct msg_acknowledge {
        uint8_t         msgID;
        uint16_t        msgSum;
};

struct msg_heartbeat {
        uint8_t         flightMode;
        uint16_t        timeStamp;
        uint16_t        batteryVoltage;
        int             commandIndex;   // XXX should be an explicitly sized type...
};

struct msg_attitude {
        int16_t         roll;
        int16_t         pitch;
        int16_t         yaw;
};

struct msg_location {
        int32_t         latitude;
        int32_t         longitude;
        int16_t         altitude;
        int16_t         groundSpeed;
        int16_t         groundCourse;
        uint16_t        timeOfWeek;
};

struct msg_pressure {
        uint16_t        pressureAltitude;
        uint16_t        airSpeed;
};

struct msg_status_text {
        uint8_t         severity;
        char            text[50];
};

struct msg_perf_report {
        uint32_t        interval;
        uint16_t        mainLoopCycles;
        uint8_t         mainLoopTime;
        uint8_t         gyroSaturationCount;
        uint8_t         adcConstraintCount;
        uint16_t        imuHealth;
        uint16_t        gcsMessageCount;
};

struct msg_command {
        int             itemNumber;
        int             listLength;
        uint8_t         commandID;
        uint8_t         p1;
        uint16_t        p2;
        uint32_t        p3;
        uint32_t        p4;
};

struct msg_get_value {
        uint8_t         valueID;
        uint8_t         broadcast;
};

struct msg_value {
        uint8_t         valueID;
        uint32_t        value;
};

struct msg_request_pid {
        uint8_t         pidSet;
};

struct msg_pid {
        uint8_t         pidSet;
        uint32_t        p;
        uint32_t        i;
        uint32_t        d;
        uint16_t        integratorMax;
};

struct msg_radio_values {
        uint16_t        values[8];      // XXX hardcoded channel max = 8
};

union msg_payload {
        struct msg_acknowledge   acknowledge;
        struct msg_heartbeat     heartbeat;
        struct msg_attitude      attitude;
        struct msg_location      location;
        struct msg_pressure      pressure;
        struct msg_status_text   status_text;
        struct msg_perf_report   perf_report;
        struct msg_command       command;
        struct msg_get_value     get_value;
        struct msg_value         value;
        struct msg_request_pid   request_pid;
        struct msg_pid           pid;
        struct msg_radio_values  radio_values;
};

struct msg_packet_header {
        uint8_t         length;
        uint8_t         messageID;
        uint8_t         messageVersion;
};

